﻿using Microsoft.Rtc.Collaboration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace MSFT_SAMPLE_ConferencingServiceLibrary
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "ConferenceSchedulingService" in both code and config file together.
    public class ConferenceSchedulingService : IConferenceSchedulingService
    {
        public static UCMAUtil util = new UCMAUtil();
  
        public string DoWork()
        {
            return "Hello WOrld";
        }


        [WebInvoke(Method = "GET", 
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json,
            UriTemplate="test")]

        public ConferenceItem TestItem()
        {
            
            ConferenceItem testItem = new ConferenceItem();
            testItem.Description = "Test";
            testItem.Organizer = "Test@user.com";
            string[] guests = { "guest1@domain.com", "guest2@domain.com" };
            testItem.ParticipantsGuests = guests;
            return testItem;
        }
        

        public List<ConferenceItem> GetConferences(string user, bool isStatic)
        {
            MeetingInstance getConferenceObject = new MeetingInstance();
            if (util.isRunning())
            {
                
                getConferenceObject.Init(user, util);
                var toReturn = getConferenceObject.GetConferences(isStatic ? SchedulingTemplate.AdministratorSupplied : SchedulingTemplate.OrganizerSupplied);
               // getConferenceObject.DestroyEndpoint();
                return toReturn;
            }
            else if (!util.isRunning())
            {
                util.CreateAndStartServerPlatform();
                
                getConferenceObject.Init(user, util);
                var toReturn = getConferenceObject.GetConferences(isStatic ? SchedulingTemplate.AdministratorSupplied : SchedulingTemplate.OrganizerSupplied);
                //getConferenceObject.DestroyEndpoint();
                return toReturn;
            }
            else
            {
                
            }
            return new List<ConferenceItem>();
        }

        public ConferenceItem CreateConference(ConferenceItem conf)
        {
            MeetingInstance createConferenceObject = new MeetingInstance();
            if (util.isRunning())
            {

                createConferenceObject.Init(conf.Organizer, util);
                var toReturn = createConferenceObject.CreatNewConference(conf);
                return toReturn;
            }
            else if (!util.isRunning())
            {
                util.CreateAndStartServerPlatform();

                createConferenceObject.Init(conf.Organizer, util);
                var toReturn = createConferenceObject.CreatNewConference(conf);
                return toReturn;
            }
            else
            {

            }
            return new ConferenceItem();
        }

        public string CancelConference(string confId, string userUri)
        {
            MeetingInstance cancelConferenceObject = new MeetingInstance();
            if (util.isRunning())
            {

                cancelConferenceObject.Init(userUri, util);
                var toReturn = cancelConferenceObject.CancelMeeting(confId);
                return toReturn;
            }
            else if (!util.isRunning())
            {
                util.CreateAndStartServerPlatform();

                cancelConferenceObject.Init(userUri, util);
                var toReturn = cancelConferenceObject.CancelMeeting(confId);
                return toReturn;
            }
            else
            {

            }
            return "Fail";
        }


        public string ResetStaticConference(string userUri)
        {
            MeetingInstance ResetStaticConferenceObject = new MeetingInstance();
            if (util.isRunning())
            {

                ResetStaticConferenceObject.Init(userUri, util);
                var toReturn = ResetStaticConferenceObject.ResetUserStaticConference(userUri);
                return toReturn;
            }
            else if (!util.isRunning())
            {
                util.CreateAndStartServerPlatform();

                ResetStaticConferenceObject.Init(userUri, util);
                var toReturn = ResetStaticConferenceObject.ResetUserStaticConference(userUri);
                return toReturn;
            }
            else
            {

            }
            return "Fail";
        }
    
    }
}
